package cl.rizepe.playntest.core.effects;

import static playn.core.PlayN.graphics;
import playn.core.CanvasImage;
import playn.core.ImageLayer;
import cl.rizepe.playntest.core.views.EffectedLayer;

public abstract class EffectLayerBlank implements Effect, OnLayerEffect {
	EffectedLayer layerState;
	ImageLayer shape=null;
	
	
	
	public EffectLayerBlank (EffectedLayer layerState){
		this.layerState=layerState;
		
		layerState.addEffect(this);
	}

	@Override
	public void apply() {	
		if (shape == null)
			drawShape();
		shape.setVisible(true);	
	}

	protected void drawShape() {
		if (shape!=null)
			shape.destroy();
		CanvasImage canvasImage =graphics().createImage(1,1);
		shape=graphics().createImageLayer(canvasImage);
		shape.setVisible(false);
		if (layerState.parent()!=null)
			layerState.parent().add(shape);
	}
	
	@Override
	public void erase() {
		shape.setVisible(false);
	}
	
	public EffectedLayer getLayerState() {
		return layerState;
	}
	
	@Override
	public void refresh() {
		boolean visibility = shape.visible();
		drawShape();
		shape.setVisible(visibility);		
	}
	
	public void setLayerState(EffectedLayer layerState) {
		this.layerState = layerState;
	}
	
	 @Override
	public void toggle() {
		shape.setVisible(!shape.visible());
	}
	
}
